<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>searchBook</title>
    <script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.6.1/jquery.min.js"></script>
    <!--    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css"-->
    <!--          integrity="sha384-HSMxcRTRxnN+Bdg0JdbxYKrThecOKuH5zCYotlSAcp1+c8xmyTe9GYg1l9a69psu" crossorigin="anonymous"/>-->
    <!--    <script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js"></script>-->
    <link rel="stylesheet" th:href="@{/bootstrap.min.css}" />
    <script th:src="@{/vue.js}"></script>
</head>
<body>

<div th:insert="publicHeader"/>
<script th:src="@{/publicHeader.js}"></script>



<div class="container-fluid" id="app" style="margin-top: 180px;margin-bottom: 100px">

<!--    searchBook-->
<!--    {{keyword}}-->


    <div class="container col-md-12" v-if="total<=0" >
        <h3 style="text-align: center">暂无 {{keyword}} 相关书籍</h3>
    </div>

    <div class="container"  v-if="total>0">

        <div class="row">
            <div class="col-md-12">
                <p style="background-color: cornflowerblue ; color: white; font-size: 30px">"{{keyword}}"搜索结果</p>
            </div>
        </div>

        <div class="row">
            <div class="col-sm-6 col-md-4" v-for="book in bookList">
                <div class="thumbnail">
                    <img :src="book.imgsrc" alt="..." class="col-sm-6 col-md-6">
                    <div class="caption text-left">
                        <p>书名:<span v-text="book.name">name</span></p>
                        <p>作者:<span v-text="book.author">author</span></p>
                        <p>价格:<span v-text="book.price">price</span></p>
                        <p>
                            <a class="btn btn-primary" :href="'/book/singleBook?bookId='+book.id">详情</a> &nbsp;&nbsp;&nbsp;&nbsp;
                            <a class="btn btn-success"  @click.prevent="addToCart(book.id)">加入购物车</a>
                        </p>
                    </div>
                </div>
            </div>
        </div>

        <div class="row">
            <div class="col-md-12 col-md-offset-5 col-lg-offset-4col-xl-offset-5" >
                <ul class="pagination justify-content-center">
                    <li><a href="javascript:void(0)" @click.prevent="go(currentPage-1)">&laquo;</a></li>
                    <li><a href="javascript:void(0)" v-for="n in totalPages" v-text="n" @click.prevent="go(n)">n</a></li>
                    <li><a href="javascript:void(0)" @click.prevent="go(currentPage+1)" >&raquo;</a></li>
                </ul>
            </div>
        </div>

    </div>



</div>

<script>

    console.log(".........searchBook...........")

    new Vue({
        el: "#app"  //vue对象托管(挂载)哪个html上, 这里我们就是id为app的元素
        ,
        data: {  //属性
            keyword:'[[${keyword}]]',
            bookList:[],
            currentPage:1,
            pageSize:3,
            total:0,
            totalPages:0,

        }
        ,
        methods: {  //方法
            initBookList(){

                let _this = this;

                let params = {
                    currentPage: this.currentPage,
                    pageSize : this.pageSize,
                    keyword: this.keyword
                }
                // $.post("/book/searchBookBySql",params,function (data) {
                $.post("/book/searchBookByES",params,function (data) {
                    console.log(data)

                    _this.bookList=data.records;
                    _this.currentPage=data.current;
                    _this.pageSize=data.size;
                    //数据总量
                    _this.total=data.total;
                    //总页数
                    _this.totalPages=data.pages;
                })
            }
            ,
            addToCart(bookId){
                console.log(bookId)


                //判断当前是否是登录状态,如果未登录,跳转到登录页
                console.log(publicHeader)
                console.log(publicHeader._data.currentAccount)

                if(!publicHeader._data.currentAccount){
                    alert("请先登录");
                    location.href="/login.html";
                    return;
                }


                //访问后端接口,把图书加入购物车
                $.get("/mycart/add?bookId="+bookId,function (data) {
                    if(data == "ok"){
                        alert("加购成功")
                    }
                })
            }
            ,
            go(pageNum){
                //小于1 或者 大于total 或者 仍获取当前页数据 直接返回
                if(pageNum<1 || pageNum>this.totalPages || pageNum==this.currentPage){
                    console.log(pageNum + " 无效页码值")
                    return;
                }

                this.currentPage = pageNum;
                this.initBookList();
            }

        }
        ,
        created(){  //声明周期中的方法,在页面加载时就会被调用

            console.log(this.keyword)
            //搜索关键词回显到搜索框中
            publicHeader._data.keyword=this.keyword;

            this.initBookList();
        }

    });


</script>


<div th:insert="publicFooter"/>
<script th:src="@{/publicFooter.js}"></script>


</body>
</html>